<template>
  <div class="login_container">
    <!-- 头像区域 -->
    <div class="loginbox">
      <div class="avater">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单区域 -->
      <div>
        <el-form ref="loginForm" :rules="loginFormRules" :model="LoginFormData" class="formtable">
          <el-form-item prop="username">
            <el-input v-model="LoginFormData.username" prefix-icon="iconfont icon-user "></el-input>
          </el-form-item>
          <!-- 校验规则是通过item上的prop属性 以及表单的rules相互搭配 -->
          <el-form-item prop="password">
            <el-input
              v-model="LoginFormData.password"
              prefix-icon="iconfont icon-3702mima"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="addlogin">登录</el-button>
            <el-button type="info" @click="resetLoginForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
           
<script>
export default {
  data() {
    return {
      LoginFormData: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 15,
            message: '用户名长度在 3 到 15 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '密码长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  methods: {
    addlogin() {
      // console.log(11);
      //   对表单进行预验证 其实已经提示预校验也可以不操作
      // 利用validate方法进行校验
      this.$refs.loginForm.validate(async valid => {
        // console.log(valid); // 是一个布尔值如果是true则说明通过

        if (!valid) {
          return this.$message.error('请输入正确的用户名和密码,谢谢！');
        } else {
          let { data: res } = await this.axios.post(
            'login',
            this.LoginFormData
          );
          console.log(res);
          if (res.meta.status !== 200) {
            return this.$message.error('用户名或密码错误，请确认');
          }
          // 登录成功后需要对token进行存储
          sessionStorage.setItem('token', res.data.token);
          this.$router.push('/home');
        }
      });

      // 开始发起axios请求
    },
    resetLoginForm() {
      // 利用ref 然后清空整个表单
      this.$refs.loginForm.resetFields();
    }
  },
  mounted() {}
};
</script>
<style lang='less' scoped>
.login_container {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  .loginbox {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .avater {
      width: 130px;
      height: 130px;
      position: absolute;
      background-color: #fff;
      border: 1px solid #eee;
      box-shadow: 0 0 10px #ddd;
      left: 50%;
      top: 0%;
      transform: translate(-50%, -50%);
      padding: 10px;
      border-radius: 50%;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
}

.loginForm {
  position: absolute;
  padding: 0 20px;
  bottom: 0px;
}
.formtable {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
